package com.example.demo.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Todolist;
import com.example.demo.entity.User;
import com.example.demo.mapper.TodolistMapper;
import com.example.demo.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService {
    @Resource
    @Autowired
    private UserMapper userMapper;
    @Transactional // 添加事务注解
    public boolean saveUser(User user) {
        int result = userMapper.insert(user);
        return result > 0; // 判断插入是否成功
    }



    @Autowired
    private TodolistMapper todolistMapper;

    public List<User> getUsersWithTodoList() {
        List<User> users = userMapper.selectList(null);
        for (User user : users) {
            List<Todolist> todos = todolistMapper.selectByUserId(user.getId());
            user.setTodoList(todos);
        }
        return users;
    }

}
